package com.opennews.openplatform

import com.opennews.openplatform.dto.IncomeExpenseDetailQueryArgument
import groovy.transform.CompileStatic
import org.springframework.http.HttpStatus

@CompileStatic
class JourneyIncomeExpenseDetailController extends BaseController {
    static allowedMethods = [
        add: "POST",
        update: "POST",
        delete: "POST"
    ]

    JourneyIncomeExpenseDetailService journeyIncomeExpenseDetailService

    // region: Get actions.
    def index() {
        render("IncomeExpenseDetailController")
    }

    def queryExistingDistinctYears() {
        def accountGroupId = getClaims().accountGroupId
        def result = journeyIncomeExpenseDetailService.queryExistingDistinctYears(accountGroupId)

        sendDataResponse(result)
    }

    def queryListByJourney() {
        def pageSize = params.int("pageSize")
        def pageIndex = params.int("pageIndex")
        def journeyId = params.journeyId.toString()
        def keyword = params.keyword.toString()
        def accountGroupId = getClaims().accountGroupId

        // Prepares the query argument.
        def queryArgument = new IncomeExpenseDetailQueryArgument(pageSize: pageSize, pageIndex: pageIndex, accountGroupId: accountGroupId, keyword: keyword)

        def result = journeyIncomeExpenseDetailService.queryListByJourney(journeyId, queryArgument)

        sendDataResponse(result)
    }

    def queryDetails() {
        def result = journeyIncomeExpenseDetailService.queryDetails(getClaims().accountGroupId, params.id.toString())

        sendDataResponse(result)
    }

    def queryCoordinates() {
        def year = params.year.toString()
        def accountGroupId = getClaims().accountGroupId
        def result = journeyIncomeExpenseDetailService.queryCoordinates(accountGroupId, year)

        sendDataResponse(result)
    }
    //endregion

    // region: Post actions.
    def add() {
        def requestBodyData = getRequestBodyData()
        def claims = getClaims()
        journeyIncomeExpenseDetailService.add(requestBodyData, claims.accountGroupId, claims.username)

        sendStatusResponse(HttpStatus.NO_CONTENT)
    }

    def update() {
        def requestBodyData = getRequestBodyData()
        def claims = getClaims()
        journeyIncomeExpenseDetailService.update(claims.accountGroupId, claims.username, requestBodyData)

        sendStatusResponse(HttpStatus.NO_CONTENT)
    }

    def delete() {
        def requestBodyData = getRequestBodyData()
        def id = requestBodyData.id.toString()
        def accountGroupId = getClaims().accountGroupId
        journeyIncomeExpenseDetailService.delete(accountGroupId, [id])

        sendStatusResponse(HttpStatus.NO_CONTENT)
    }
    //endregion
}